package com.kgcx.system.controller;

import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.controller.BaseController;
import com.kgcx.system.domain.SysEmployee;
import com.kgcx.system.domain.SysEmployeeCity;
import com.kgcx.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(description = "系统用户", tags = "系统用户接口")
@RestController
@RequestMapping("sys/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "获取用户列表", httpMethod = "GET", notes = "获取用户列表接口")
    @RequestMapping(value = "getSysUserPageList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页容量", required = true, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "account", value = "账号", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "姓名", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "gender", value = "性别", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "phone", value = "电话", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    public ReturnData<?> getSysUserPageList(
            @RequestParam(value = "pageNum", required = true) Integer pageNum,
            @RequestParam(value = "pageSize", required = true) Integer pageSize,
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "gender", required = false) Integer gender,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "userId", required = false) Long userId) {
        SysEmployee user = new SysEmployee();
        user.setAccount(account);
        user.setName(name);
        user.setGender(gender);
        user.setPhone(phone);
        user.setKid(userId);
        return userService.getSysUserPageList(user, pageNum, pageSize);
    }

    @ApiOperation(value = "系统用户登录", httpMethod = "POST", notes = "系统用户登录接口")
    @RequestMapping(value = "sysUserlogin", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "passWord", value = "密码", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)})
    public ReturnData<?> sysUserlogin(@RequestParam(value = "account", required = true) String account,
                                      @RequestParam(value = "passWord", required = true) String passWord) {
        return new ReturnData<>(userService.sysUserlogin(account, passWord));
    }

    @ApiOperation(value = "删除系统用户", httpMethod = "POST", notes = "删除系统用户接口")
    @RequestMapping(value = "deleteUserByUserId", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class)})
    public ReturnData<?> deleteUserByUserId(@RequestParam(value = "userId", required = true) Long userId) {
        userService.deleteUser(userId);
        return new ReturnData<>();
    }

    @ApiOperation(value = "添加系统用户", httpMethod = "POST", notes = "添加系统用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "帐号", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "名字", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "gender", value = "性别 1男 2女", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "roleIds", value = "角色集合，多个逗号隔开(1,3,4)", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "cityCodes", value = "城市集合，多个逗号隔开(1,3,4)", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @RequestMapping(value = "addSysUser", method = RequestMethod.POST)
    public ReturnData<?> addSysUser(@RequestParam(value = "account", required = true) String account,
                                    @RequestParam(value = "password", required = true) String password,
                                    @RequestParam(value = "name", required = true) String name,
                                    @RequestParam(value = "gender", required = true) Integer gender,
                                    @RequestParam(value = "phone", required = false) String phone,
                                    @RequestParam(value = "roleIds", required = false) String roleIds,
                                    @RequestParam(value = "cityCodes", required = false) String cityCodes) {
        SysEmployee user = new SysEmployee();
        user.setAccount(account);
        user.setPassword(password);
        user.setName(name);
        user.setGender(gender);
        user.setPhone(phone);
        return userService.addSysUser(user, roleIds, cityCodes);
    }

    @RequestMapping(value = "updateUserByUserId", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class),
//		@ApiImplicitParam(name = "account", value = "帐号", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "名字", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "gender", value = "性别", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "roleIds", value = "角色集合，多个逗号隔开(1,3,4)", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "cityCodes", value = "城市集合，多个逗号隔开(1,3,4)", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @ApiOperation(value = "修改用户信息", httpMethod = "POST", notes = "修改用户信息接口")
    public ReturnData<?> updateUserByUserId(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "gender", required = false) Integer gender,
            @RequestParam(value = "phone", required = false) String phone,
//			@RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "password", required = false) String password,
            @RequestParam(value = "roleIds", required = false) String roleIds,
            @RequestParam(value = "cityCodes", required = false) String cityCodes) {
        SysEmployee user = new SysEmployee();
        user.setKid(userId);
        user.setName(name);
        user.setGender(gender);
        user.setPhone(phone);
        //user.setAccount(account);
        user.setPassword(password);
        userService.updateUserByKid(user, roleIds, cityCodes);
        return new ReturnData<>();
    }

    @ApiOperation(value = "userID获取用户信息", httpMethod = "GET", notes = "userID获取用户信息接口")
    @GetMapping("getUserByUserkid")
    public ReturnData<SysEmployee> getUser() {
        SysEmployee user = userService.getUserByUserkid(getRequestData());
        return new ReturnData<>(user);
    }

    @ApiOperation(value = "userID获取用户信息", httpMethod = "GET", notes = "userID获取用户信息接口")
    @GetMapping("getUserByAccount")
    public ReturnData<SysEmployee> getUserByAccount(@RequestParam String account) {
        SysEmployee user = userService.getUserByAccount(account);
        return new ReturnData<>(user);
    }

    @ApiOperation(value = "根据用户ID得到该用户是否有url权限", httpMethod = "GET", notes = "根据用户ID得到该用户是否有url权限")
    @GetMapping("roleAuth")
    public ReturnData<Boolean> roleAuth(
            @RequestParam(value = "employeeId", required = true) Long employeeId,
            @RequestParam(value = "url", required = true) String url) {
        Boolean bool = userService.roleAuth(getRequestData());
        return new ReturnData<>(bool);
    }

    @ApiOperation(value = "根据员工ID获取到城市列表", httpMethod = "GET", notes = "根据员工ID获取到城市列表接口")
    @GetMapping("getCityCodeByEmployeeId")
    public ReturnData<List<SysEmployeeCity>> getCityCodeByEmployeeId() {
        List<SysEmployeeCity> sysEmployeeCity = userService.getCityCodeByEmployeeId(getRequestData());
        return new ReturnData<>(sysEmployeeCity);
    }

    @ApiOperation(value = "根据员工account获取到城市列表", httpMethod = "GET", notes = "根据员工account获取到城市列表接口")
    @GetMapping("getCityCodeByAccount")
    public ReturnData<List<SysEmployeeCity>> getCityCodeByAccount() {
        List<SysEmployeeCity> sysEmployeeCity = userService.getCityCodeByAccount(getRequestData());
        return new ReturnData<>(sysEmployeeCity);
    }


}
